package com.crm.service.statisticAnalysis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.model.common.dtos.PageResult;
import com.crm.model.common.dtos.ResponseResult;
import com.crm.model.common.enums.AppHttpCodeEnum;
import com.crm.model.statisticAnalysis.dtos.CluePageDTO;
import com.crm.model.statisticAnalysis.pojos.ClueStatistics;
import com.crm.model.statisticAnalysis.vos.ClueDoughnutChart;
import com.crm.model.statisticAnalysis.vos.ClueLineChartVO;
import com.crm.service.statisticAnalysis.mapper.ClueMapper;
import com.crm.service.statisticAnalysis.service.ClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ClueServiceImpl implements ClueService {
    @Autowired
    private ClueMapper clueMapper;

    /**
     * 线索分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult cluePage(CluePageDTO dto) {
        if (StringUtils.isEmpty(dto)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        IPage<ClueStatistics> page = new Page<>(dto.getPage(), dto.getPageSize());
        LambdaQueryWrapper<ClueStatistics> lqw = new LambdaQueryWrapper<>();

        if (dto.getClueNumber() != null && !dto.getClueNumber().equals("")) {
            lqw.like(ClueStatistics::getClueNumber, dto.getClueNumber());
        }

        if (dto.getDepartment() != null) {
            lqw.eq(ClueStatistics::getDepartment, dto.getDepartment());
        }

        if (dto.getName() != null && !dto.getName().equals("")) {
            lqw.like(ClueStatistics::getBelongerName, dto.getName());
        }

        page = clueMapper.selectPage(page, lqw);

        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getRecords());

        return ResponseResult.okResult(pageResult);
    }

    /**
     * 线索统计折线图
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public ResponseResult clueLineChart(LocalDate begin, LocalDate end) {
        //时间集合
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);

        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        //返回VO
        List<ClueLineChartVO> lineChartVOS = new ArrayList<>();

        //遍历每一天
        for (LocalDate date : dateList) {
            //一天最小时间 和 最大时间
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            //查询每天总线索数
            Integer totalClueCount = clueMapper.getClueCount(null, endTime);
            //查询每天新增
            Integer newClueCount = clueMapper.getClueCount(beginTime, endTime);

            ClueLineChartVO lineChartVO = ClueLineChartVO.builder()
                    .newClueCount(newClueCount)
                    .totalClueCount(totalClueCount)
                    .date(date)
                    .build();

            lineChartVOS.add(lineChartVO);
        }

        return ResponseResult.okResult(lineChartVOS);
    }

    /**
     * 线索统计漏斗图
     */
    @Override
    public ResponseResult clueDoughnutChart(LocalDate begin, LocalDate end) {
        //时间集合
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);

        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        //返回vo
        List<ClueDoughnutChart> clueDoughnut = new ArrayList<>();

        //总线索数
        Integer clueCount = clueMapper.selectCount(null);

        for (LocalDate date : dateList) {
            //一天最小时间 和 最大时间
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            //查询每天新增
            Integer newClueCount = clueMapper.getClueCount(beginTime, endTime);

            //线索转换率 新增线索 / 总线索数
            double clueConversion = 0.0;
            if (clueCount != 0) {
                clueConversion = newClueCount / clueCount.doubleValue();
            }

            ClueDoughnutChart doughnutChart = new ClueDoughnutChart();
            doughnutChart.setClueCount(clueCount);
            doughnutChart.setClueConversion(clueConversion);
            doughnutChart.setDate(date);

            clueDoughnut.add(doughnutChart);
        }
        Collections.sort(clueDoughnut);
        //取前四个
        List<ClueDoughnutChart> collect = clueDoughnut.stream().limit(4).collect(Collectors.toList());

        return ResponseResult.okResult(collect);
    }
}
